package com.bjxst.dao.impl;

import com.bjxst.commons.JdbcUtils;
import com.bjxst.dao.UserloginDao;
import com.bjxst.pojo.Users;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

//实现查询用户登录
public class UserLoginDaoImpl implements UserloginDao {
    @Override
    public Users selectUserByUserNameAndUserPwd(String username, String userpwd) {
        //在while外面定义users   方便判断用户有没有查询到
        Users users = null;
        Connection conn = null;
        try{
            //获得connection 对象
            conn = JdbcUtils.getConnection();
            //给定查询语句  prepareStatement
            PreparedStatement ps = conn.prepareStatement("select * from users where username=? and userpwd=?");
            //绑定 上面的问号
            ps.setString(1,username);
            ps.setString(2,userpwd);
            ResultSet resultSet = ps.executeQuery();
            //将结果映射到users对象中
            while(resultSet.next()){
                users = new Users();
                users.setUserid(resultSet.getInt("userid"));
                users.setUsername(resultSet.getString("username"));
                users.setUserpwd(resultSet.getString("userpwd"));
                users.setUsersex(resultSet.getString("usersex"));
                users.setPhonenumber(resultSet.getString("phonenumber"));
                users.setQqnumber(resultSet.getString("Qqnumber"));

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JdbcUtils.closeConnection(conn);
        }


        return users;
    }
}
